package com.jiyun.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.C;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.CourseMapper;
import com.jiyun.mapper.QuizMapper;
import com.jiyun.mapper.RaceMapper;
import com.jiyun.mapper.TrainMapper;
import com.jiyun.model.Course;
import com.jiyun.model.Quiz;
import com.jiyun.model.Race;
import com.jiyun.model.Train;
import com.jiyun.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/* *
 * @Author nxj
 * @Date 2023 08 2023/8/4 10 43
 * */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private RaceMapper raceMapper;

    @Autowired
    private TrainMapper trainMapper;

    @Autowired
    private QuizMapper quizMapper;

    @Override
    public List<Long> showCount(Integer id) {
//        统计竞赛人数
        ArrayList<Long> list = new ArrayList<>();
        LambdaQueryWrapper<Race> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Race::getUid,id);
        Long count = raceMapper.selectCount(lambdaQueryWrapper);

//        统计培训人数
        LambdaQueryWrapper<Train> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Train::getUid,id);
        List<Train> list1 = trainMapper.selectList(lambdaQueryWrapper1);
        long a=0;
        for (Train train : list1) {
            String[] split = train.getCid().split(",");
             a=split.length+a;
        }
        list.clear();
        list.add(count);
        list.add((long)list1.size());
        list.add(a);
        return list;
    }

    @Override
    public List<Course> courseList() {
//        统计课程信息
        ArrayList<Course> list1 = new ArrayList<>();

        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Course::getGood);
        List<Course> list = courseMapper.selectList(lambdaQueryWrapper);

        list1.clear();

        for (int i = 0; i < 3; i++) {
            list1.add(list.get(i));
        }

        return list1;
    }


    @Override
    public List<Course> courseNum() {
        ArrayList<Course> list = new ArrayList<>();

//        统计课程人数
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Course::getCreatetime);
        List<Course> courses = courseMapper.selectList(lambdaQueryWrapper);

        list.clear();
        for (int i = 0; i < 3; i++) {
            list.add(courses.get(i));
        }

        return list;
    }
}
